<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/3/22
 * Time: 上午11:49
 */
class RechargeController extends AdminBaseController {
	
	public function grade(){
		$this->setTitle('充值等级');
		
		$filter = new PubFilter('pl');
		$chart  = new PubChart();
		$grid   = new PubGrid();
		
		$lod_special_id = $this->R('special_id');
		$lodos = $this->R('os');
		$filter->addSpecialFilter(false,2);
		$filter->addOsFilter($lod_special_id,true,2)
        ->addServerFilter($lod_special_id,$lodos,true,false,2)
		->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-7 day'))),2)
		->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))),2);
		//->addChannel();
		$params = $filter->get();
		
		$params = $this->indexCommon($params);
		
		$header = RechargeService::gradeHeader($params);
		$data   = RechargeService::gradeData($params);
		

		$grid->addGrid($header, $data, array(
				'name'  => '充值等级',
				'order' => array('level', 'desc'),
				'fix'   => array(
						'col' => 1
				)
		));
		
		$gradeCountchart = RechargeService::gradeCountchart($data, $params);		
		$chart->addChart('gradeCountchart', $gradeCountchart['data'], array(
				
				'name'   => PubFunc::t("等级充值次数分布"),
				'config' => $gradeCountchart['config']
		));
		
		$gradeGoldchart = RechargeService::gradeGoldchart($data, $params);
		$chart->addChart('gradeGoldchart', $gradeGoldchart['data'], array(
				'name'   => PubFunc::t("等级充值元宝分布"),
				'config' => $gradeGoldchart['config']
		));
		
		
		$chart->setShowType("float");
		$this->out['filter']    = $filter->show();
		$this->out['grid']      = $grid->show();
		$this->out['chart']     = $chart->show();
		$this->out['is_ana'] = 2;
	}

    public function index(){
        $this->setTitle('用户充值报表');

        $filter = new PubFilter('pl');
        $chart  = new PubChart();
        $grid   = new PubGrid();

        $lod_special_id = $this->R('special_id');
		$lodos = $this->R('os');
		$filter->addSpecialFilter(false,2);
		$filter->addOsFilter($lod_special_id,true,2)
        ->addServerFilter($lod_special_id,$lodos,true,false,2)
            ->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-7 day'))),2)
            ->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))),2);

        $params = $filter->get();
        
        $params = $this->indexCommon($params);

        $params['period'] = 'Daily';
        if($params['show'] == 'ByGame') $params['time_end'] = $params['time_start'];

        $header = RechargeService::header($params);
        $data   = RechargeService::data($params);

        $grid->addGrid($header, $data, array(
            'name'  => '用户充值报表',
            'order' => array('RecDate', 'desc'),
        	'fix'   => array(
        			'col' => 1
        	)
        ));

        $chartConfig = RechargeService::chart($data, $params);
        
        $chart->addChart('chart', $chartConfig['data'], array(
            'name'   => $params['time_start'].'~'.$params['time_end'].PubFunc::t('用户充值报表'),
            'config' => $chartConfig['config']
        ));
        
        $this->out['filter']    = $filter->show();
        $this->out['grid']      = $grid->show();
        $this->out['chart']     = $chart->show();
        $this->out['is_ana'] = 2;
    }

    public function ltv(){
        $this->setTitle('用户LTV报表');

        $filter = new PubFilter('pl');
        $grid   = new PubGrid();

        $lod_special_id = $this->R('special_id');
		$lodos = $this->R('os');
		$filter->addSpecialFilter(false,2);
		$filter->addOsFilter($lod_special_id,true,2)
        ->addServerFilter($lod_special_id,$lodos,true,false,2)
            ->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-7 day'))),2)
            ->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))),2);

        $params = $filter->get();
        
        $params = $this->indexCommon($params);

        $params['period'] = 'Daily';
        if($params['show'] == 'ByGame') $params['time_end'] = $params['time_start'];

        $header = RechargeService::ltvHeader($params);
        $data   = RechargeService::ltvData($params);

        $grid->addGrid($header, $data, array(
            'name'  => '用户LTV报表',
            'order' => array('RecDate', 'desc'),
        	'fix'   => array(
        			'col' => 2
        	)
        ));

        $this->out['filter']    = $filter->show();
        $this->out['grid']      = $grid->show();
        $this->out['is_ana'] = 2;
    }

    public function user(){
        $this->setTitle('用户付费数据');

        $filter = new PubFilter('pl');
        $grid   = new PubGrid();

        $filter->addOsFilter()
            ->addServerFilter(true)
            ->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-7 day'))))
            ->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))))
            ->addExport()
        ;

        $params = $filter->get();
        $params['period'] = 'Daily';
        if($params['show'] == 'ByGame') $params['time_end'] = $params['time_start'];

        $header = RechargeService::userHeader($params);
        $data   = RechargeService::userData($params);

        if($filter->isExport()) {
            $filename = LibExcel::getFilename('用户付费数据', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else{
            $grid->addGrid($header, $data, array(
                'name'  => '用户付费数据',
                'order' => array('RecDate', 'desc')
            ));

            $this->out['filter']    = $filter->show();
            $this->out['grid']      = $grid->show();
        }
    }

    public function retain(){
        $this->setTitle('付费用户数据分析');

        $filter = new PubFilter('pl');
        $grid   = new PubGrid();

        $filter->addOsFilter()
            ->addServerFilter(true)
            ->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-7 day'))))
            ->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))))
            ->addExport()
        ;

        $params = $filter->get();
        $params['period'] = 'Daily';
        if($params['show'] == 'ByGame') $params['time_end'] = $params['time_start'];

        $header = RechargeService::retainHeader($params);
        $data   = RechargeService::retainData($params);

        if($filter->isExport()) {
            $filename = LibExcel::getFilename('付费用户数据分析', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else{
            $grid->addGrid($header, $data, array(
                'name'  => '付费用户数据分析',
                'order' => array('RecDate', 'desc')
            ));

            $this->out['filter']    = $filter->show();
            $this->out['grid']      = $grid->show();
        }
    }
    
    public function interval(){
    	$this->setTitle('用户充值区间');
    
    	$filter = new PubFilter('pl');
    	$grid   = new PubGrid();
    
    	$lod_special_id = $this->R('special_id');
    	$lodos = $this->R('os');
    	$filter->addSpecialFilter(false,2);
    	$filter->addOsFilter($lod_special_id,true,2)
    	->addServerFilter($lod_special_id,$lodos,true.false,2)
    	->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-7 day'))),2)
    	->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))),2);
    
    	$params = $filter->get();
    
    	$params = $this->indexCommon($params);

    	$params['period'] = 'Daily';

    
    	$header = RechargeService::intervalHeader($params);
    	$data   = RechargeService::intervalData($params);
    	foreach ($data as $key=>$val){
    		
    		$grid->addGrid($header, $val, array(
    				'name'  => $key,
    				'order' => array('RecDate', 'desc'),
    				'fix'   => array(
    						'col' => 1
    				)
    		));
    	}
    	$grid->setShowType("line");
    	$this->out['filter']    = $filter->show();
    	$this->out['grid']      = $grid->show();
    	$this->out['is_ana'] = 2;
    }
    
    
    
    
}